<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUserAddressTable extends Migration
{
    public $tableName = "user_address";

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if(!Schema::hasTable($this->tableName)) $this->create();
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->tableName);
    }

    /**
     * Run the migrations.
     *
     * @return void
     */
    private function create()
    {
        Schema::create($this->tableName, function (Blueprint $table) {
            $table->engine = 'InnoDB';      // 设置存储引擎
            $table->charset = 'utf8';       // 设置字符集
            $table->collation  = 'utf8_general_ci';       // 设置排序规则

            $table->increments('address_id')->comment('主键ID');
            $table->string('name', 30)->nullable(false)->default('')->comment('收货人姓名');
            $table->string('phone', 20)->nullable(false)->default('')->comment('联系电话');
            $table->unsignedInteger('user_id')->nullable(false)->default(0)->index('user_id')->comment('用户ID');
            $table->unsignedInteger('province_id')->nullable(false)->default(0)->comment('省份ID');
            $table->unsignedInteger('city_id')->nullable(false)->default(0)->comment('城市ID');
            $table->unsignedInteger('region_id')->nullable(false)->default(0)->comment('区/县ID');
            $table->string('province')->nullable(false)->default('')->comment('省份');
            $table->string('city')->nullable(false)->default('')->comment('城市');
            $table->string('region')->nullable(false)->default('')->comment('区/县');
            $table->string('detail')->nullable(false)->default('')->comment('详细地址');
            $table->unsignedTinyInteger('is_delete')->default(2)->index('is_delete')->comment('是否删除：1=是    2=否');
            $table->unsignedTinyInteger('is_default')->default(2)->index('is_default')->comment('是否默认：1=是    2=否');
            $table->timestamps();
        });

        $prefix = DB::getConfig('prefix');
        $qu = "ALTER TABLE " . $prefix . $this->tableName . " comment '用户收货地址表'";
        DB::statement($qu);
    }

}
